package com.midian.service.sys.impl;

import java.io.Serializable;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.midian.common.dao.BaseDaoTemplet;
import com.midian.common.dao.OrderHelper;
import com.midian.common.dao.SearchHelper;
import com.midian.common.dao.SearchHelper.Type;
import com.midian.common.exception.BizException;
import com.midian.common.pager.Pager;
import com.midian.model.sys.ValidateCode;
import com.midian.service.common.BaseService;
import com.midian.service.sys.IValidateCodeService;

/**
 * 验证码业务实现类
 * @author Mengfh
 *
 */
@Service("validateCodeService")
public class ValidateCodeServiceImpl extends BaseService implements IValidateCodeService {
	/**验证码DAO*/
	private BaseDaoTemplet<ValidateCode> validateCodeDAO;
	
	@Resource(name = "validateCodeDAO")
	public void setValidateCodeDAO(BaseDaoTemplet<ValidateCode> validateCodeDAO) {
		this.validateCodeDAO = validateCodeDAO;
	}

	@Override
	public ValidateCode add(ValidateCode target) throws BizException {
		this.validateCodeDAO.add(target);
		return target;
	}

	@Override
	public void update(ValidateCode target) throws BizException {
		this.validateCodeDAO.updateByConvertHql(target);
	}

	@Override
	public void delete(Serializable id) throws BizException {
		this.validateCodeDAO.delete(id);
	}

	@Override
	public void delete(Serializable[] ids) throws BizException {
		this.validateCodeDAO.delete(ids);
	}

	@Override
	public ValidateCode get(Serializable id) throws BizException {
		return this.validateCodeDAO.get(id);
	}

	@Override
	public List<ValidateCode> showList(ValidateCode target, Pager pager, List<OrderHelper> helper) throws BizException {
		return this.validateCodeDAO.find(target, getSearchHelper(), helper, pager);
	}

	@Override
	public void opDelFlag(Serializable[] ids, Integer status) throws BizException {
		this.validateCodeDAO.opDelFlag(ids, status);
	}

	@Override
	protected List<SearchHelper> getSearchHelper() {
		String[] attrs = {};
		Type[] types = {};
		return SearchHelper.getSearchList(attrs, types);
	}
	
	
	@Override
	public boolean isExistCode(String phone, String code) {
		String hql = "select count(o) from ValidateCode o where o.phone = ? and o.code = ? and timestampdiff(minute, o.createTime, now()) <= 5";
		Object[] params = new Object[]{phone, code};
		boolean result = false;
		if(this.validateCodeDAO.getCount(hql, params) > 0) {
			result = true;
		}
		return result;
		//return "111111".equals(code);
	}
	
	@Override
	public boolean isExistCodeAndDelete(String phone, String code) {
		String hql = "select count(o) from ValidateCode o where o.phone = ? and o.code = ? and timestampdiff(minute, o.createTime, now()) <= 5";
		Object[] params = new Object[]{phone, code};
		boolean result = false;
		if(this.validateCodeDAO.getCount(hql, params) > 0) {
			hql = "delete from ValidateCode o where o.phone = ? and o.code = ?";
			this.validateCodeDAO.update(hql, params);
			result = true;
		}
		return result;
		//return "111111".equals(code);
	}

	@Override
	public void saveOrUpdateCode(String phone, String code) throws BizException {
		//如果电话已经申请过验证码，更新验证码
				if(isExistCode(phone, code)) {
					String hql = "update ValidateCode o set o.code = ? where o.phone = ?";
					this.validateCodeDAO.update(hql, new Object[]{code, phone});
				} else {
				//如果电话没有申请过验证码，插入验证码
					ValidateCode validateCode = new ValidateCode();
					validateCode.setPhone(phone);
					validateCode.setCode(code);
					this.validateCodeDAO.add(validateCode);
				}
		
	}
}
